package org.apache.commons.dbcp;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
 * 连接池
 * @author Kettas
 */
public class DataSourceFactory extends BasicDataSource {
	private String JNDI = null;
	/**
	 * 重写方法增加获得系统自定义连接池的方法(注入)
	 */
	public Connection getConnection() throws SQLException {
		if (JNDI != null) {
			if (dataSource == null) {
				try {
					dataSource = (DataSource) new InitialContext().lookup(JNDI);
				} catch (NamingException e) {
					throw new SQLException("连接数据库失败(" + e.getMessage() + ")!",
							e);
				}
			}
		} else {
			if (dataSource == null) {
				dataSource = createDataSource();
			}
		}
		return dataSource.getConnection();
	}
	public String getJNDI() {
		return JNDI;
	}
	public void setJNDI(String jndi) {
		JNDI = jndi;
	}
}
